package org.leaf.green.admin

class GroupController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/admin/group/list', model : [groupInstanceList: Group.list(params), groupInstanceTotal: Group.count()]
    }

    def create = {
        def groupInstance = new Group()
        groupInstance.properties = params
        render view : '/admin/group/create', model : [groupInstance: groupInstance]
    }

    def save = {
        def groupInstance = new Group(params)
        if (!groupInstance.hasErrors() && groupInstance.save()) {
            flash.message = "group.created"
            flash.args = [groupInstance.id]
            flash.defaultMessage = "Group ${groupInstance.id} created"
            redirect(action: "show", id: groupInstance.id)
        }
        else {
            render(view: "/admin/group/create", model: [groupInstance: groupInstance])
        }
    }

    def show = {
        def groupInstance = Group.get(params.id)
        if (!groupInstance) {
            flash.message = "group.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Group not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/admin/group/show', model :[groupInstance: groupInstance]
        }
    }

    def edit = {
        def groupInstance = Group.get(params.id)
        if (!groupInstance) {
            flash.message = "group.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Group not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/admin/group/edit', model : [groupInstance: groupInstance]
        }
    }

    def update = {
        def groupInstance = Group.get(params.id)
        if (groupInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (groupInstance.version > version) {
                    
                    groupInstance.errors.rejectValue("version", "group.optimistic.locking.failure", "Another user has updated this Group while you were editing")
                    render(view: "/admin/group/edit", model: [groupInstance: groupInstance])
                    return
                }
            }
            groupInstance.properties = params
            if (!groupInstance.hasErrors() && groupInstance.save()) {
                flash.message = "group.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Group ${params.id} updated"
                redirect(action: "show", id: groupInstance.id)
            }
            else {
                render(view: "/admin/group/edit", model: [groupInstance: groupInstance])
            }
        }
        else {
            flash.message = "group.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Group not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def groupInstance = Group.get(params.id)
        if (groupInstance) {
            try {
                groupInstance.delete()
                flash.message = "group.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Group ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "group.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Group ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "group.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Group not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
